<?php
$dbms='mysql';     //数据库类型
//$host='localhost'; //数据库主机名
$host='127.0.0.1'; //数据库主机名
$dbName='copton';    //使用的数据库
$user='root';      //数据库连接用户名
$pass='MyNewPass4!';          //对应的密码
//$pass='root';          //对应的密码
$dsn="$dbms:host=$host;dbname=$dbName";

$res = [
    'status'=>0
];

function get_empty_oil( $model_id, $dbh ){
    $sql = 'select * from empty_oil where model_id="' . $model_id . '"';
    $sth = $dbh->prepare( $sql );
    $sth->execute();
    $empty_oil = $sth->fetch(PDO::FETCH_ASSOC);
    $result = array();
    if( !empty( $empty_oil ) && !empty( $empty_oil['oil_code'] )  ){
        $sql = 'select * from oil where code="' . $empty_oil['oil_code'] . '"';
        $sth = $dbh->prepare( $sql );
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_ASSOC);
    }
    return !empty( $result ) ? $result : array();
}
function change_gearbox_oil( $old_oil , $dbh ){

    $sql = 'select * from oil where copton_name="' . $old_oil . '"';
    $sth = $dbh->prepare( $sql );
    $sth->execute();
    $result = $sth->fetch(PDO::FETCH_ASSOC);
//    return !empty( $result ) ? $result['name'] : $old_oil;
    return !empty( $result ) ? $result : array();
}

if(!empty( $_POST ) && !empty($_POST['modelsid'])  ){
    $model_id = $_POST['modelsid'];

    $sql = "select * from model_info where model_id='{$model_id}'";



    try {
        $dbh = new PDO($dsn, $user, $pass, array(PDO::MYSQL_ATTR_INIT_COMMAND => "set names utf8")); //初始化一个PDO对象

        $sth = $dbh->prepare( $sql );
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_ASSOC);
//        print_r($result);

        if( !empty( $result ) ){
            $res['data'] = array();
            $engine = array(
                'classname'=>'发动机油',
                'capacity'=> $result['engine_capacity']=='0.0' ? '': $result['engine_capacity'],
//                'description'=>$result['engine_description'],
                'description'=>'',
//                'seriesname'=>$result['engine_oil_name'],
                'seriesname'=>"全合成型发动机油  <a href='http://feixue.1005858.com/a/aipaikexilie__qiyoufadongjiyou/?from=singlemessage'>更多请点击</a>",
            );

            $res['data'][] = $engine;
            if( !empty( $result['gearbox_oil_name']) ){
                $oil_info = change_gearbox_oil( $result['gearbox_oil_name'], $dbh );
            }else{
                $oil_info = get_empty_oil( $result['model_id'], $dbh );
            }

            $oil_name = '';
            $oil_description = '';
            $oil_code = '';
            if( !empty( $oil_info ) ){
                $oil_name = $oil_info['name'];
                $oil_description = $oil_info['description'];
                $oil_code = $oil_info['code'];
            }
            $gearbox = array(
                'classname'=>'变速箱油',
                'capacity'=>$result['gearbox_capacity']=='0.0'?'':$result['gearbox_capacity'],
//                'description'=>$result['gearbox_description'],
                'description'=>'',
                'seriesname'=>$oil_name . ' ' . $oil_description,
                'oil_name'=>$oil_name,
                'oil_description'=>$oil_description,
                'oil_code'=>$oil_code
            );

            $res['data'][] = $gearbox;


            $res['status'] = 1;
            $res['msg'] = '查询到相关信息！';
        }


        $dbh = null;
    } catch (PDOException $e) {

        $res['error'] = "Error!: " . $e->getMessage() . "<br/>";
//        die ("Error!: " . $e->getMessage() . "<br/>");
    }
}

if( empty( $res['data']) ){
    $res['msg'] = "对不起，没有查询到符合条件的信息！";
}
echo json_encode( $res );
